/***************************************************************************
 创建者：田昱民
 开始时间：               : 2016.11.1
 copyright            : (C) 同川科技
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 修改人:林鸿铭
 修改时间:2016.11.28
 修改说明:工程整合

 ***************************************************************************
 *   示教器主程序
 *                                                                         *
 ***************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      ui(new Ui::MainWindow)
{


    ui->setupUi(this);

    this->setGeometry(rect().x(), rect().y(),600, 880);
    this->setFixedSize(600, 880);
    //this->setStyleSheet("outline: none;");            //无虚线框

    this->setWindowFlags(Qt::FramelessWindowHint);    //无边框
    this->setAttribute(Qt::WA_QuitOnClose, true);

   ui->comboBox->addItem("T1");
   ui->comboBox->addItem("T2");


   timer = new QTimer();
   connect(timer,SIGNAL(timeout()),this,SLOT(timeroutslot()));//timeoutslot()为自定义槽
   timer->start(300);

    //msgBox
    hmiMsgBox = new HmiMessageBoxDialog;

    //膜
    film = NULL;

    pointType=1;
    ui->radioButton_2->setChecked(true);
    ui->radioButton->setChecked(false);

    ui->radioButton_7->setChecked(true);
    do1=false;
    do2=false;
}

MainWindow::~MainWindow()
{
    qDebug()<<"*****umount*****";
    if(0==system("umount /tcrwork/linuxController/trunk/TcrController"))
    {
        qDebug()<<"*****umount ok*****";
    }
    else
    {
        qDebug()<<"*****umount false*****";
    }

}



void MainWindow::setShowInitialLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/tcrwork/linuxControllerHmi/trunk/hmiController/language/mainWindowEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
    hmiMsgBox->changeLanguage();
}

void MainWindow::showMsgBoxTypeSlot(int msgBoxType,QString title,QString text)
{



    hmiMsgBox->hideAllShow();
    if(msgBoxType == EM_MSG_BOX_ALL_BTN)
    {
        hmiMsgBox->messageBoxShowAllBtn(title ,text);
    }
    else if(msgBoxType == EM_MSG_BOX_SURE_BTN)
    {
        hmiMsgBox->messageBoxShowSureBtn(title ,text);
    }
    else if(msgBoxType == EM_MSG_BOX_NO_BTN)
    {
        hmiMsgBox->messageBoxShowWithoutBtn(title ,text);
    }
}

void MainWindow::showMsgBoxNeedReturnTypeSlot(int msgBoxType, int target, QString title, QString text)
{


    hmiMsgBox->messageBoxShowNeedReturn(msgBoxType,target,title,text);
}

void MainWindow::closeMsgBoxSlot()
{
    hmiMsgBox->recvMessageBoxHide();
}

void MainWindow::showFilmSlot()
{
    if(film)
    {
        film->setGeometry(0,0,600,800);
        film->show();
    }
}

void MainWindow::hideFilmSlot()
{
    if(film)
    {
        film->hide();
    }
}

void MainWindow::changeLanguageSlot(int language)
{
    setShowInitialLanguage(language);
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    switch(event->type())
    {
    case QEvent::MouseButtonPress:
        //case QEvent::KeyPress:
    case QEvent::TouchBegin:
    {
//        int nMiniDlgCurrentId = EM_DIF_HIDE_ALL;
        QPoint ReleasePos;
        ReleasePos = QCursor::pos();

//        m_pCustomInstace->m_emPressFlag = EM_PDU_DOWN;
//        m_pCustomInstace->m_emMouseFlag = EM_PDU_DOWN;

        ReleasePos.setX(ReleasePos.x()-70);
        ReleasePos.setY(ReleasePos.y()-26);

        /**键盘显示 start*/
        //2016.11.24 修改 支持按键点击空白处退出
        //QWidget *current_focus_widget;
        QMainWindow *current_focus_widget;
        //current_focus_widget = QApplication::focusWidget();//(QWidget *)obj;
        current_focus_widget = (QMainWindow *)obj;
        if(current_focus_widget->objectName().compare("qt_scrollarea_viewport") == 0)
        {
            current_focus_widget = (QMainWindow *)current_focus_widget->parent();
        }


        if(!current_focus_widget)
        {
            return QWidget::eventFilter(obj, event);
        }
        if(current_focus_widget->inherits("QPushButton"))
        {

            {

            }


        }
        else
        {

        }

        /**键盘显示 end*/

        if(current_focus_widget->inherits("HmiFilmLabel"))//自定义的一层隔膜
        {
            //qDebug()<<"main window";
            hideFilmSlot();

            emit hideAllTopView();
        }


    }
        break;
    case QEvent::MouseButtonRelease:
        //case QEvent::KeyRelease:
    case QEvent::TouchEnd:
    {
        QPoint ReleasePos;
        ReleasePos = QCursor::pos();

        ReleasePos.setX(ReleasePos.x()-70);
        ReleasePos.setY(ReleasePos.y()-26);

    }
        break;
    case QEvent::MouseButtonDblClick:
    {
        QPoint ReleasePos;
        ReleasePos = QCursor::pos();

        ReleasePos.setX(ReleasePos.x()-70);
        ReleasePos.setY(ReleasePos.y()-26);


    }
        break;
    case QEvent::MouseMove:
    {
        QPoint ReleasePos;
        ReleasePos = QCursor::pos();
    }
        break;
    case QEvent::KeyPress:
    {
    }
        break;
    default:
        break;

    }
    return QWidget::eventFilter(obj, event);
}


//电机使能
void MainWindow::on_checkBox_clicked()
{
    int isServoOn=ui->checkBox->isChecked();


    communicateSaveThread->setManualServonSlot(isServoOn);

}

//清除报警
void MainWindow::on_pushButton_clicked()
{
    communicateNormalThread->cleanAllMsgSlot();
}


void MainWindow::on_comboBox_currentIndexChanged(int index)
{

}


void MainWindow::on_horizontalScrollBar_rangeChanged(int min, int max)
{

}


//手动速度调整
void MainWindow::on_horizontalScrollBar_sliderMoved(int position)
{
   communicateNormalThread->setManualSpeedSlot(position) ;
}

//切换Ｔ１　Ｔ２
void MainWindow::on_comboBox_activated(int index)
{
    communicateNormalThread->setControlMaunalModeSlot(index);
}

//DO控制
void MainWindow::on_radioButton_5_clicked()
{
        do1=!do1;
        ui->radioButton_5->setChecked(do1);
        communicateNormalThread->setDoSlot(0,do1);

}
//DO控制
void MainWindow::on_radioButton_6_clicked()
{
    do2=!do2;
    ui->radioButton_6->setChecked(do2);
    communicateNormalThread->setDoSlot(1,do2);

}

//DI显示及状态
void MainWindow::timeroutslot()
{
//       globalData->ioIn[i];
       //qDebug()<<"time ";

       //状态数据更新
       if(globalData)
       {
           if(globalData->tryLock() )
           {

                   if(0==globalData->globalCurrentState)
                   {
                      ui->label_3->setText("cease");
                   }
                   if(1==globalData->globalCurrentState)
                   {
                      ui->label_3->setText("run");
                   }
                   if(2==globalData->globalCurrentState)
                   {
                      ui->label_3->setText("stop");
                   }

                   if(globalData->messageVector.size()>0)
                   {
//                       ui->label_8->setText(globalData->messageVector.at(0)  );
                       ui->label_8->setText("error ocurs ~" );
                   }
                   else
                   {
                       ui->label_8->setText("normal" );
                   }

               globalData->unlock();
           }
       }


       //IO 数据
       int diCount;
       if(ioData)
       {
           if(ioData->tryLock() )
           {

                   diCount = ioData->ioIn.size();
                   if(diCount > 0)
                   {
                       ui->radioButton_3->setChecked(ioData->ioIn[0]);
                       ui->radioButton_4->setChecked(ioData->ioIn[1]);

                   }

               ioData->unlock();
           }
       }


}

//停止
void MainWindow::on_pushButton_12_clicked()
{
    communicateSaveThread->stopRun();
}



void MainWindow::on_radioButton_7_clicked()
{
    communicateNormalThread->setJointCoordinateSlot(EM_JOG_COORDINATE_JOINT);
    ui->radioButton_7->setChecked(true);
    ui->radioButton_8->setChecked(false);
    ui->radioButton_9->setChecked(false);
    ui->radioButton_10->setChecked(false);

}

void MainWindow::on_radioButton_8_clicked()
{
    communicateNormalThread->setJointCoordinateSlot(EM_JOG_COORDINATE_WORLD);
    ui->radioButton_7->setChecked(false);
    ui->radioButton_8->setChecked(true);
    ui->radioButton_9->setChecked(false);
    ui->radioButton_10->setChecked(false);
}

void MainWindow::on_radioButton_9_clicked()
{
    communicateNormalThread->setJointCoordinateSlot(EM_JOG_COORDINATE_TOOL);
    ui->radioButton_7->setChecked(false);
    ui->radioButton_8->setChecked(false);
    ui->radioButton_9->setChecked(true);
    ui->radioButton_10->setChecked(false);
}

void MainWindow::on_radioButton_10_clicked()
{
    communicateNormalThread->setJointCoordinateSlot(EM_JOG_COORDINATE_USER);
    ui->radioButton_7->setChecked(false);
    ui->radioButton_8->setChecked(false);
    ui->radioButton_9->setChecked(false);
    ui->radioButton_10->setChecked(true);
}

void MainWindow::on_pushButton_4_pressed()
{
    //qDebug("pushButton_R1_pressed");
    int axisNumber = EM_AXIS_1;
    int direction = EM_DIRECTION_LEFT;

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);

}

void MainWindow::on_pushButton_4_released()
{
    moveOperation->stopJogMove();
}

void MainWindow::on_pushButton_5_pressed()
{
    //qDebug("pushButton_R1_pressed");
    int axisNumber = EM_AXIS_1;
    int direction = EM_DIRECTION_RIGHT;

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);

}

void MainWindow::on_pushButton_5_released()
{
    moveOperation->stopJogMove();
}


void MainWindow::on_pushButton_7_pressed()
{
    //qDebug("pushButton_R1_pressed");
    int axisNumber = EM_AXIS_2;
    int direction = EM_DIRECTION_LEFT;

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void MainWindow::on_pushButton_7_released()
{
    moveOperation->stopJogMove();
}

void MainWindow::on_pushButton_6_pressed()
{
    //qDebug("pushButton_R1_pressed");
    int axisNumber = EM_AXIS_2;
    int direction = EM_DIRECTION_RIGHT;

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void MainWindow::on_pushButton_6_released()
{
    moveOperation->stopJogMove();
}

void MainWindow::on_pushButton_9_pressed()
{
    //qDebug("pushButton_R1_pressed");
    int axisNumber = EM_AXIS_3;
    int direction = EM_DIRECTION_LEFT;

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void MainWindow::on_pushButton_9_released()
{
    moveOperation->stopJogMove();
}

void MainWindow::on_pushButton_8_pressed()
{
    //qDebug("pushButton_R1_pressed");
    int axisNumber = EM_AXIS_3;
    int direction = EM_DIRECTION_RIGHT;

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void MainWindow::on_pushButton_8_released()
{
    moveOperation->stopJogMove();
}

void MainWindow::on_pushButton_11_pressed()
{
    //qDebug("pushButton_R1_pressed");
    int axisNumber = EM_AXIS_4;
    int direction = EM_DIRECTION_LEFT;

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void MainWindow::on_pushButton_11_released()
{
    moveOperation->stopJogMove();
}

void MainWindow::on_pushButton_10_pressed()
{
    //qDebug("pushButton_R1_pressed");
    int axisNumber = EM_AXIS_4;
    int direction = EM_DIRECTION_RIGHT;

    //jogTimer->start(JOG_TIME);
    //新调用方式
    moveOperation->startJogMove(axisNumber,direction);
}

void MainWindow::on_pushButton_10_released()
{
    moveOperation->stopJogMove();
}

//修改ＰＲ点数据　坐标
void MainWindow::on_pushButton_13_clicked()
{

}

//修改ＰＲ点数据　关节
void MainWindow::on_pushButton_14_clicked()
{

}

//struct PointPro
//{
//    int jointFlag;                  //0:关节;1:笛卡尔;
//    int pointNum;                   //点 编号
//    int groupNum;                   //组 编号
//    int ufValue;                    //用户坐标系 编号
//    int utValue;                    //工具坐标系 编号
//    int recordStatus;                  //在使用用户坐标系，或者是工具坐标系进行坐标系示教的时候（三点法，四点法，六点法），记录该点的示教状态，０代表未示教　　１代表已经示教
//    std::string configString;       //多解 选择;多圈 选择
//    std::vector<int> configInt;     //多解 选择;多圈 选择
//    std::vector<float> positionValue;//关节值 or 笛卡尔值
//    std::vector<float> extPositionValue;//外部轴的值，仅存在关节形式
//};

//关节移动到点开始
void MainWindow::on_pushButton_2_pressed()
{
    //新尝试,直接赋值过去
    MoveParameter tmpPoint;
    tmpPoint.velocity=30;
    tmpPoint.moveType = 0;//0:关节;1:直线;
    tmpPoint.pointType = 2;//0 普通P点 1 码垛 2　PR寄存器点

    tmpPoint.targetPoint.positionValue.resize(6);//HUALEI 4改为６
    if(1==pointType)
    {
        tmpPoint.targetPoint.jointFlag=1;//0:关节;1:迪卡尔;
        tmpPoint.targetPoint.positionValue[0]=ui->lineEdit_2->text().toFloat();
        tmpPoint.targetPoint.positionValue[1]=ui->lineEdit_3->text().toFloat();
        tmpPoint.targetPoint.positionValue[2]=ui->lineEdit_4->text().toFloat();
        tmpPoint.targetPoint.positionValue[3]=0;
        tmpPoint.targetPoint.positionValue[4]=0;
        tmpPoint.targetPoint.positionValue[5]=ui->lineEdit_5->text().toFloat();
    }
    else
    {
        tmpPoint.targetPoint.jointFlag=0;//0:关节;1:迪卡尔;
        tmpPoint.targetPoint.positionValue[0]=ui->lineEdit_18->text().toFloat();
        tmpPoint.targetPoint.positionValue[1]=ui->lineEdit_19->text().toFloat();
        tmpPoint.targetPoint.positionValue[2]=ui->lineEdit_20->text().toFloat();
        tmpPoint.targetPoint.positionValue[3]=ui->lineEdit_21->text().toFloat();
        tmpPoint.targetPoint.positionValue[4]=0;
        tmpPoint.targetPoint.positionValue[5]=0;

    }




    //2017-04-13 (不需要补充,已有)补充判断:有无点数据
    if(tmpPoint.targetPoint.positionValue.size() <= 0)
    {
        return;
    }

    moveOperation->startPointMove(tmpPoint);

}
//关节移动到点停止
void MainWindow::on_pushButton_2_released()
{
    moveOperation->stopPointMove();

}
//直线移动到点
void MainWindow::on_pushButton_3_pressed()
{
    //新尝试,直接赋值过去
    MoveParameter tmpPoint;
    //tmpPoint.velocity=5;
    tmpPoint.moveType = 1;//0:关节;1:直线;
    tmpPoint.pointType = 2;//0 普通P点 1 码垛 2　PR寄存器点

    tmpPoint.targetPoint.positionValue.resize(4);
    if(1==pointType)
    {
        tmpPoint.targetPoint.jointFlag=1;//0:关节;1:迪卡尔;
        tmpPoint.targetPoint.positionValue[0]=ui->lineEdit_2->text().toFloat();
        tmpPoint.targetPoint.positionValue[1]=ui->lineEdit_3->text().toFloat();
        tmpPoint.targetPoint.positionValue[2]=ui->lineEdit_4->text().toFloat();
        tmpPoint.targetPoint.positionValue[3]=ui->lineEdit_5->text().toFloat();
    }
    else
    {
        tmpPoint.targetPoint.jointFlag=0;//0:关节;1:迪卡尔;
        tmpPoint.targetPoint.positionValue[0]=ui->lineEdit_18->text().toFloat();
        tmpPoint.targetPoint.positionValue[1]=ui->lineEdit_19->text().toFloat();
        tmpPoint.targetPoint.positionValue[2]=ui->lineEdit_20->text().toFloat();
        tmpPoint.targetPoint.positionValue[3]=ui->lineEdit_21->text().toFloat();
    }




    //2017-04-13 (不需要补充,已有)补充判断:有无点数据
    if(tmpPoint.targetPoint.positionValue.size() <= 0)
    {
        return;
    }

    moveOperation->startPointMove(tmpPoint);
}
//直线移动到点停止
void MainWindow::on_pushButton_3_released()
{
    moveOperation->stopPointMove();
}

void MainWindow::on_radioButton_2_clicked()
{
    pointType=1;
    ui->radioButton_2->setChecked(true);
    ui->radioButton->setChecked(false);
}

void MainWindow::on_radioButton_clicked()
{
    pointType=0;
    ui->radioButton_2->setChecked(false);
    ui->radioButton->setChecked(true);
}
